package com.hz.monitoring.module.hardware.service.mcu_tenant_resource;

import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.module.hardware.controller.admin.mcu_tenant_resource.vo.*;
import com.hz.monitoring.module.hardware.convert.mcu_tenant_resource.McuTenantResourceConvert;
import com.hz.monitoring.module.hardware.dal.dataobject.mcu_tenant_resource.McuTenantResourceDO;
import com.hz.monitoring.module.hardware.dal.mysql.mcu_tenant_resource.McuTenantResourceMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.hz.monitoring.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.hz.monitoring.module.hardware.enums.ErrorCodeConstants.MCU_TENANT_RESOURCE_NOT_EXISTS;

/**
 * MCU租户配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class McuTenantResourceServiceImpl implements McuTenantResourceService {

    @Resource
    private McuTenantResourceMapper mcuTenantResourceMapper;

    @Override
    public Long createMcuTenantResource(McuTenantResourceCreateReqVO createReqVO) {
        // 插入
        McuTenantResourceDO mcuTenantResource = McuTenantResourceConvert.INSTANCE.convert(createReqVO);
        mcuTenantResourceMapper.insert(mcuTenantResource);
        // 返回
        return mcuTenantResource.getId();
    }

    @Override
    public void updateMcuTenantResource(McuTenantResourceUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateMcuTenantResourceExists(updateReqVO.getId());
        // 更新
        McuTenantResourceDO updateObj = McuTenantResourceConvert.INSTANCE.convert(updateReqVO);
        mcuTenantResourceMapper.updateById(updateObj);
    }

    @Override
    public void deleteMcuTenantResource(Long id) {
        // 校验存在
        this.validateMcuTenantResourceExists(id);
        // 删除
        mcuTenantResourceMapper.deleteById(id);
    }

    private void validateMcuTenantResourceExists(Long id) {
        if (mcuTenantResourceMapper.selectById(id) == null) {
            throw exception(MCU_TENANT_RESOURCE_NOT_EXISTS);
        }
    }

    @Override
    public McuTenantResourceDO getMcuTenantResource(Long id) {
        return mcuTenantResourceMapper.selectById(id);
    }

    @Override
    public List<McuTenantResourceDO> getMcuTenantResourceList(McuTenantResourceListReqVO listReqVO) {
        return mcuTenantResourceMapper.selectList(listReqVO);
    }

    @Override
    public PageResult<McuTenantResourceDO> getMcuTenantResourcePage(McuTenantResourcePageReqVO pageReqVO) {
        return mcuTenantResourceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<McuTenantResourceDO> getMcuTenantResourceList(McuTenantResourceExportReqVO exportReqVO) {
        return mcuTenantResourceMapper.selectList(exportReqVO);
    }

}
